"""
@Author: li
@Email: lijianqiao2906@live.com
@FileName: base.py
@DateTime: 2025/07/05
@Docs: 基础响应模式
"""

from typing import Any

from pydantic import BaseModel, Field


class BaseResponse[DataType](BaseModel):
    """基础响应模式"""

    code: int = Field(default=200, description="响应代码")
    message: str = Field(default="成功", description="响应消息")
    data: DataType | None = Field(default=None, description="响应数据")


class BaseRequest(BaseModel):
    """基础请求模式"""

    class Config:
        extra = "forbid"
        use_enum_values = True


class PaginationRequest(BaseModel):
    """分页请求模式"""

    page: int = Field(default=1, ge=1, description="页码")
    page_size: int = Field(default=10, ge=1, le=100, description="每页大小")

    class Config:
        extra = "forbid"


class SoftDeleteRequest(BaseModel):
    """软删除请求模式"""

    include_deleted: bool = Field(default=False, description="是否包含已删除的数据")

    class Config:
        extra = "forbid"


class RestoreRequest(BaseModel):
    """恢复请求模式"""

    ids: list[int] = Field(description="要恢复的ID列表", min_length=1)

    class Config:
        extra = "forbid"


class BulkDeleteRequest(BaseModel):
    """批量删除请求模式"""

    ids: list[int] = Field(description="要删除的ID列表", min_length=1)
    hard_delete: bool = Field(default=False, description="是否硬删除")

    class Config:
        extra = "forbid"


class PaginatedResponse[DataType](BaseModel):
    """分页响应模式"""

    code: int = Field(default=200, description="响应代码")
    message: str = Field(default="成功", description="响应消息")
    data: list[DataType] = Field(default_factory=list, description="响应数据")
    total: int = Field(default=0, description="总记录数")
    page: int = Field(default=1, description="当前页码")
    page_size: int = Field(default=10, description="每页大小")

    @property
    def total_pages(self) -> int:
        """计算总页数"""
        if self.page_size <= 0:
            return 0
        return (self.total + self.page_size - 1) // self.page_size

    @property
    def has_next(self) -> bool:
        """是否有下一页"""
        return self.page < self.total_pages

    @property
    def has_prev(self) -> bool:
        """是否有上一页"""
        return self.page > 1


class ErrorResponse(BaseModel):
    """错误响应模式"""

    code: int = Field(description="错误代码")
    message: str = Field(description="错误消息")
    detail: Any = Field(default=None, description="错误详情")


class SoftDeletedDataResponse[DataType](BaseModel):
    """软删除数据响应"""

    active_data: list[DataType] = Field(default_factory=list, description="活跃数据")
    deleted_data: list[DataType] = Field(default_factory=list, description="已删除数据")
    active_count: int = Field(default=0, description="活跃数据数量")
    deleted_count: int = Field(default=0, description="已删除数据数量")


class OperationResponse(BaseModel):
    """操作结果响应"""

    code: int = Field(default=200, description="响应代码")
    message: str = Field(default="操作成功", description="响应消息")
    success_count: int = Field(default=0, description="成功数量")
    failed_count: int = Field(default=0, description="失败数量")
    failed_items: list[dict[str, Any]] = Field(default_factory=list, description="失败项详情")


# 常用响应类型
SuccessResponse = BaseResponse[dict[str, Any]]
ListResponse = BaseResponse[list[dict[str, Any]]]
